package com.example.demo02.controller.Redis;

import com.example.demo02.service.RandomService2;
import com.example.demo02.utils.R;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Log4j2
public class RandomController2 {

    @Resource
    private RandomService2 randomService2;

    @Resource
    private RedisTemplate<String, Integer> redisTemplate; // 假设泛型是String和Integer

    @GetMapping("/random/paynum")
    @ResponseBody
    public R randomData(Integer num) {
        try {
            List<Integer> persons = this.redisTemplate.opsForSet().randomMembers(randomService2.RANDOM_SET_KEY, num);
            return R.ok().data("persons", persons).message("抽到奖品的员工有：" + persons);
        } catch (Exception ex) {
            log.error("抽奖失败", ex);
            return R.error().message("服务忙!!!");
        }
    }
}